﻿using CompanyManageSystem.UI.Info;
using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CompanyManageSystem.UI.Main
{
    public partial class Frm_Main : Form
    {
        #region 构造方法
        /// <summary>
        /// 构造方法
        /// </summary>
        public Frm_Main()
        {
            InitializeComponent();
        }
        #endregion

        #region 窗体加载完成
        /// <summary>
        /// 窗体加载完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Frm_Main_Load(object sender, EventArgs e)
        {
            barButtonItem6.Caption = GetIpAddress();

            this.BackColor = Color.LightGray;

            Frm_User frm = new Frm_User();
            frm.TopLevel = false;
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }
        #endregion

        /// <summary>
        /// BarManager合并事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barManager1_Merge(object sender, BarManagerMergeEventArgs e)
        {
            //子窗口中的BarManager中的Tools工具条
            Bar childBar = e.ChildManager.Bars["Tools"];
            //父窗口中的BarManager中的Tools工具条
            Bar parentBar = barManager1.Bars["Tools"];
            if (childBar != null)
            {
                //把子窗口中的Tools工具条添加到父窗口中的Tools工具条
                parentBar.Merge(childBar);
            }
        }

        /// <summary>
        /// BarManager拆分事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barManager1_UnMerge(object sender, BarManagerMergeEventArgs e)
        {
            //只显示父窗口中的Tools工具条()
            Bar parentBar = barManager1.Bars["Tools"];
        }

        /// <summary>
        /// 子窗口选择判断
        /// </summary>
        /// <param name="frmName"></param>
        /// <returns></returns>
        public bool MdiIsActivity(string frmName)
        {
            bool bl = false;
            foreach (Form childFrom in this.MdiChildren)
            {
                //获取子窗口的名称
                if (childFrom.GetType().Name == frmName)
                {
                    //显示子窗口
                    //如果当前窗口已经打开，则显示就行了，不需要创建新的窗口
                    childFrom.Visible = true;
                    childFrom.Activate();
                    bl = true;
                }
            }
            return bl;
        }

        /// <summary>
        /// 用户管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!MdiIsActivity("Frm_User"))
            {
                Frm_User frm = new Frm_User();
                frm.TopLevel = false;
                frm.MdiParent = this;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
        }

        /// <summary>
        /// 角色管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (!MdiIsActivity("Frm_Company"))
            {
                Frm_Company frm = new Frm_Company();
                //不可嵌套在其它页面
                frm.TopLevel = false; 
                //设置父窗体为Frm_Main
                frm.MdiParent = this;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
        }

        /// <summary>
        /// 管理员
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            Frm_Manager frm = new Frm_Manager();
            frm.ShowDialog();
        }

        /// <summary>
        /// 获取本机IP地址
        /// </summary>
        /// <returns></returns>
        private string GetIpAddress()
        {
            string hostName = Dns.GetHostName();   //获取本机名
            IPHostEntry localhost = Dns.GetHostByName(hostName);    //方法已过期，可以获取IPv4的地址
            //IPHostEntry localhost = Dns.GetHostEntry(hostName);   //获取IPv6地址
            IPAddress localaddr = localhost.AddressList[0];

            return localaddr.ToString();
        }

    }
}
